package ldh.im.rpc.codec;

import ldh.im.rpc.buffer.RpcBuffer;
import ldh.serialize.ObjectSerialized;

/**
 * 使用开源项目https://gitee.com/ldh123/StupidBear进行序列化和反序列化
 * 编码方式是二进制
 *
 */
public class RpcStupidBearBinaryCodec implements ObjectCodec<RpcBuffer> {

    @Override
    public <T>void encode(RpcBuffer rpcBuffer, T data) {
        byte[] bytes = ObjectSerialized.toBytes(data);
        rpcBuffer.writeBytes(bytes);
    }

    @Override
    public <T>T decode(RpcBuffer rpcBuffer, Class<T> clazz)  {
        return ObjectSerialized.toObject(clazz, rpcBuffer.array(), rpcBuffer.getReadIdx(), rpcBuffer.getWriteIdx());
    }

}
